package com.sgb.zhaocai.viewmodel;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.MapLongAndLatEntity;
import com.sgb.zhaocai.entity.ProjectIssueBean;
import com.sgb.zhaocai.entity.PublicTypeEntity;
import com.sgb.zhaocai.entity.PublishDetailEntity;
import com.sgb.zhaocai.entity.PublishListBean;
import com.sgb.zhaocai.view.demand.activity.DemandIssueActivity;
import com.sgb.zhaocai.view.demand.activity.ProjectIssueActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.entity.MenuEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/9/1
 * AUTHOR:F_Q
 * Description:
 */
public class ProjectIssueModel extends ViewModel {
    private String[] SourcesFunding = {"国拨和自筹资金", "专项资金", "市场回款", "银行融资", "援疆资金", "全额中央预算内资金", "扶贫资金", "政府发债资金", "财政资金", "中央内资金", "其他"};

    /**
     * 申请单位列表
     */
    public void getCompanyList(ProjectIssueActivity activity, View view, int type) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setCompanyList(data.getData(), view, type);
                    } else {
                        MToast.showToast(activity, "当前用户没有其它公司信息");
                    }
                }
            }
        });
    }

    /**
     * 申请部门列表
     *
     * @param activity
     * @param applyCompany
     * @param view
     */
    public void getDepartmentList(ProjectIssueActivity activity, String applyCompany, View view, int type) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeptList(applyCompany).enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setDepartmentList(data.getData(), view, type);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 工程类型
     *
     * @param activity
     * @param view
     */
    public void getProjectTypeList(ProjectIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getProjectTypeList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setProjectTypeList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 资金来源
     *
     * @param activity
     * @param view
     */
    public void getSourcesFundingList(ProjectIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        List<PublishListBean> sources = new ArrayList<>();
        for (int i = 0; i < SourcesFunding.length; i++) {
            PublishListBean listBean = new PublishListBean();
            listBean.setName(SourcesFunding[i]);
            sources.add(listBean);
        }
        DialogHelper.dismissProgressDialog();
        activity.setSourcesFundingList(sources, view);
    }

    /**
     * 经营类型
     *
     * @param activity
     * @param view
     */
    public void getBusinessTypeList(ProjectIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        List<PublishListBean> sources = new ArrayList<>();
        PublishListBean listBean1 = new PublishListBean();
        listBean1.setName("合作管理");
        sources.add(listBean1);
        PublishListBean listBean2 = new PublishListBean();
        listBean2.setName("组织经营");
        sources.add(listBean2);
        DialogHelper.dismissProgressDialog();
        activity.setBusinessTypeList(sources, view);
    }

    /**
     *材料使用管理监控
     * @param activity
     * @param view
     */
    public void getMaterialMonitoringList(ProjectIssueActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        List<PublishListBean> sources = new ArrayList<>();
        PublishListBean listBean1 = new PublishListBean();
        listBean1.setName("分部工程");
        listBean1.setId("1");
        sources.add(listBean1);
        PublishListBean listBean2 = new PublishListBean();
        listBean2.setName("单位工程");
        listBean2.setId("2");
        sources.add(listBean2);
        PublishListBean listBean3 = new PublishListBean();
        listBean3.setName("单项工程");
        listBean3.setId("3");
        sources.add(listBean3);
        DialogHelper.dismissProgressDialog();
        activity.setMaterialMonitoringList(sources, view);
    }

    /**
     * 地址信息
     * @param activity
     */
    public void getAddressModel(ProjectIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setAddressList(data.getData());
                }
            }
        });
    }

    /**
     * 获取经纬度信息
     * @param activity
     * @param provinceNo
     * @param cityNo
     * @param areaNo
     */
    public void getLocationLL(ProjectIssueActivity activity, String provinceNo, String cityNo, String areaNo) {
        String areaCode = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.AppProvinceCode, "");
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLocationLL(areaCode,provinceNo,cityNo,areaNo).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.updateAddress(data.getData());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 项目立项-新增
     * @param activity
     * @param bean
     */
    public void saveOrUpdateProject(ProjectIssueActivity activity, ProjectIssueBean bean) {
        Log.e("tag项目发布",bean.toString() );
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().saveOrUpdateProject(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setSaveOrUpdateProject(bean.getAuditStatus());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 用户列表信息
     * @param activity
     * @param view
     * @param departmentId
     * @param type
     */
    public void getUserList(ProjectIssueActivity activity, View view, String departmentId, int type) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getPersonnel(departmentId).enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setUserList(data.getData(), view, type);
                    } else {
                        MToast.showToast(activity, data.getMsg());
                    }
                }
            }
        });
    }

    /**
     * 项目编辑详情
     * @param id
     */
    public void getProjectinfo(ProjectIssueActivity activity, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDetail(id).enqueue(new ICallBack<BaseEntity<PublishDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PublishDetailEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData()!= null) {
                        activity.setProjectinfo(data.getData());
                    } else {
                        MToast.showToast(activity, data.getMsg());
                    }
                }
            }
        });
    }

    /**
     * 详情
     * @param activity
     * @param id
     */
    public void getWebH5(ProjectIssueActivity activity, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("FProjectDetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id;
                    activity.onSuccess(webUrl);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }
    /**
     * 获取商品列表
     */
    public void getGoodsCategory(ProjectIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getGoodsCategory("").enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.showGoodsTypePop(data.getData());
                }
            }
        });
    }

    /**
     * 机械租赁列表
     */
    public void getMachineTypeModel(ProjectIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMachineOutType().enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setMachineTypeModel(data.getData());
                }
            }
        });
    }

    /**
     * 设备列表
     */
    @SuppressLint("CheckResult")
    public void getParentDeviceType(ProjectIssueActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeviceType("0").enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setParentDeviceType(data.getData());
                    }
                }
            }
        });
    }
}
